package com.higgins.eventtracking.client.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;
import com.higgins.eventtracking.client.rpc.FixedOutgoingService;
import com.higgins.eventtracking.client.rpc.FixedOutgoingServiceAsync;

public class FixedOutgoingTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(FixedOutgoingTable.class.getName());
	private final FixedOutgoingServiceAsync fixedOutgoingService = GWT
			.create(FixedOutgoingService.class);
	
	public FixedOutgoingTable(FixedOutgoingColumns columns)	{
		super(columns);
		retrieveDTOs();
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<FixedOutgoingDTO> fixedOutgoingDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (FixedOutgoingDTO fixedOutgoingDTO:fixedOutgoingDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(fixedOutgoingDTO.getId());
			row.add(fixedOutgoingDTO.getOutgoing());
			row.add(fixedOutgoingDTO.getDayOfMonth());
			row.add(fixedOutgoingDTO.getAmount());
			row.add(fixedOutgoingDTO.getTotal());
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		FixedOutgoingDTO fixedOutgoingDTO = new FixedOutgoingDTO();
		fixedOutgoingDTO.setId(getId(row));
		fixedOutgoingDTO.setOutgoing(getText(row, 1));
		fixedOutgoingDTO.setDayOfMonth(getInteger(row, 2));
		fixedOutgoingDTO.setAmount(getDouble(row, 3));
		fixedOutgoingDTO.setTotal(getDouble(row, 4));
		updateRow(fixedOutgoingDTO);
	}
	
	protected void retrieveDTOs()	{
		try {
			fixedOutgoingService.retrieveFixedOutgoings(new AsyncCallback<List<FixedOutgoingDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<FixedOutgoingDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 
	}	
	private void updateRow(FixedOutgoingDTO fixedOutgoingDTO)	{
		System.out.println("updating fixedOutgoingDTO " + fixedOutgoingDTO.getId());
		try {
			fixedOutgoingService.updateFixedOutgoing(fixedOutgoingDTO, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		
	}
}
